﻿using System;
using System.Text;

namespace AutoDb {
	/// <summary>
	/// 
	/// </summary>
	sealed class CodeTextWriter : System.IO.StreamWriter {
		private int m_indent;

		private bool m_newLine;

		/// <summary>
		/// 
		/// </summary>
		public void Indent() {
			++m_indent;
		}

		/// <summary>
		/// 
		/// </summary>
		public void Unindent() {
			--m_indent;

			if (m_indent < 0) {
				m_indent = 0;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		private void WriteIndent() {
			for (int i = 0; i < m_indent; ++i) {
				base.Write("    ");
			}

			m_newLine = false;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(bool value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(char value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="buffer"></param>
		public override void Write(char[] buffer) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(buffer);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="buffer"></param>
		/// <param name="index"></param>
		/// <param name="count"></param>
		public override void Write(char[] buffer, int index, int count) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(buffer, index, count);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(decimal value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(double value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(float value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(int value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(long value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(object value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg0"></param>
		public override void Write(string format, object arg0) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(format, arg0);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg0"></param>
		/// <param name="arg1"></param>
		public override void Write(string format, object arg0, object arg1) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(format, arg0, arg1);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg0"></param>
		/// <param name="arg1"></param>
		/// <param name="arg2"></param>
		public override void Write(string format, object arg0, object arg1, object arg2) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(format, arg0, arg1, arg2);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg"></param>
		public override void Write(string format, params object[] arg) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(format, arg);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(string value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(uint value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void Write(ulong value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.Write(value);
		}

		/// <summary>
		/// 
		/// </summary>
		public override void WriteLine() {
			base.WriteLine();

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(bool value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(char value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="buffer"></param>
		public override void WriteLine(char[] buffer) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(buffer);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="buffer"></param>
		/// <param name="index"></param>
		/// <param name="count"></param>
		public override void WriteLine(char[] buffer, int index, int count) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(buffer, index, count);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(decimal value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(double value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(float value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(int value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(long value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(object value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg0"></param>
		public override void WriteLine(string format, object arg0) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(format, arg0);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg0"></param>
		/// <param name="arg1"></param>
		public override void WriteLine(string format, object arg0, object arg1) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(format, arg0, arg1);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg0"></param>
		/// <param name="arg1"></param>
		/// <param name="arg2"></param>
		public override void WriteLine(string format, object arg0, object arg1, object arg2) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(format, arg0, arg1, arg2);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="format"></param>
		/// <param name="arg"></param>
		public override void WriteLine(string format, params object[] arg) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(format, arg);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(string value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(uint value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="value"></param>
		public override void WriteLine(ulong value) {
			if (m_newLine) {
				WriteIndent();
			}

			base.WriteLine(value);

			m_newLine = true;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fileName"></param>
		public CodeTextWriter(string fileName)
			: base(fileName, false, Encoding.UTF8) {
			m_indent = 0;
			m_newLine = true;
		}
	} // end of CodeTextWriter.
}
